package org.streets.workflow.engine;

import org.streets.commons.error.NestedRuntimeException;
import org.streets.workflow.model.WFElement;
import org.streets.workflow.model.WFProcess;


public class WorkflowException extends NestedRuntimeException {

	private static final long serialVersionUID = 5541214716605894109L;
	
	/**
     * 抛出异常的流程实例的Id
     */
    String processInstanceId = null;
    /**
     * 抛出异常的流程定义的Id
     */
    String processId = null;
    /**
     * 抛出异常的流程的名称
     */
    String processName = null;
    /**
     * 抛出异常的流程的显示名称
     */
    String processDisplayName = null;
    /**
     * 抛出异常的流程元素的Id
     */
    String workflowElementId = null;
    /**
     * 抛出异常的流程元素的名称
     */
    String workflowElementName = null;
    /**
     * 抛出异常的流程元素的显示名称
     */
    String workflowElementDisplayName = null;
    /**
     * 
     * @param error
     */
    public WorkflowException(String error) {
        super(error);
    }
	/**
	 * 
	 * @param processInstance 发生异常的流程实例
	 * @param workflowElement 发生异常的流程环节或者Task
	 * @param errMsg 错误信息
	 */
    public WorkflowException(IProcessInstance processInstance, WFElement workflowElement, String errMsg) {
        super(errMsg);
        if (processInstance != null) {
            this.setProcessInstanceId(processInstance.getId());
            this.setProcessId(processInstance.getDefinitionId());
            this.setProcessName(processInstance.getName());
            this.setProcessDisplayName(processInstance.getName());
        }
        if (workflowElement != null) {
            this.setWorkflowElementId(workflowElement.getId());
            this.setWorkflowElementName(workflowElement.getName());
            this.setWorkflowElementDisplayName(workflowElement.getDisplayName());
        }
    }


    /**
     * 
     * @param processInstanceId 发生异常的流程实例Id
     * @param process 发生异常的流程
     * @param workflowElementId 发生异常的环节或者Task的Id
     * @param errMsg 错误信息
     */
    public WorkflowException(String processInstanceId, WFProcess process,
            String workflowElementId, String errMsg) {
        this(null, null, errMsg);
        this.setProcessInstanceId(processInstanceId);
        if (process != null) {
            this.setProcessId(process.getId());
            this.setProcessName(process.getName());
            this.setProcessDisplayName(process.getDisplayName());

            WFElement workflowElement = process.findWFElementById(workflowElementId);
            if (workflowElement != null) {
                this.setWorkflowElementId(workflowElement.getId());
                this.setWorkflowElementName(workflowElement.getName());
                this.setWorkflowElementDisplayName(workflowElement.getDisplayName());
            }
        }
    }

    public String getProcessId() {
        return processId;
    }

    public void setProcessId(String processId) {
        this.processId = processId;
    }

    public String getProcessInstanceId() {
        return processInstanceId;
    }

    public void setProcessInstanceId(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    public String getProcessName() {
        return processName;
    }

    public void setProcessName(String processName) {
        this.processName = processName;
    }

    public String getWorkflowElementId() {
        return workflowElementId;
    }

    public void setWorkflowElementId(String workflowElementId) {
        this.workflowElementId = workflowElementId;
    }

    public String getWorkflowElementName() {
        return workflowElementName;
    }

    public void setWorkflowElementName(String workflowElementName) {
        this.workflowElementName = workflowElementName;
    }

    public String getProcessDisplayName() {
        return processDisplayName;
    }

    public void setProcessDisplayName(String processDisplayName) {
        this.processDisplayName = processDisplayName;
    }

    public String getWorkflowElementDisplayName() {
        return workflowElementDisplayName;
    }

    public void setWorkflowElementDisplayName(String workflowElementDisplayName) {
        this.workflowElementDisplayName = workflowElementDisplayName;
    }
}
